package com.sfh.dog.ui;

import java.io.File;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.sfh.dog.DogApplication;
import com.sfh.dog.R;
import com.sfh.dog.entity.Daily;
import com.sfh.dog.entity.User;
import com.sfh.dog.util.LogUtil;

/**
 * 发布
 * @author sWX229906
 *
 */
public class AddDatilActivity extends APhotoFileActivity
{
    private ImageView ivPhoto;
    
    private String path;
    
    private View pBLoad;
    
    private EditText tvDeil;
    
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        setContentView(R.layout.add_daily_layout);
        find();
    }
    
    private void find()
    {
        ivPhoto = (ImageView)findViewById(R.id.ivPhoto);
        pBLoad = findViewById(R.id.pBLoad);
        findViewById(R.id.ivSend).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                openPhotoLib();
            }
        });
        findViewById(R.id.ivRight).setOnClickListener(new OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                pBLoad.setVisibility(View.VISIBLE);
                final BmobFile bmobFile = new BmobFile(new File(path));
                bmobFile.upload(AddDatilActivity.this, new UploadFileListener()
                {
                    
                    @Override
                    public void onSuccess()
                    {
                        User bmobUser = BmobUser.getCurrentUser(AddDatilActivity.this, User.class);
                        Daily daily = new Daily();
                        daily.setDescription(tvDeil.getText().toString());
                        daily.setPicture(bmobFile.getFileUrl());
                        daily.setDogID(bmobUser.getDogID());
                        daily.save(AddDatilActivity.this, new SaveListener()
                        {
                            @Override
                            public void onSuccess()
                            {
                                pBLoad.setVisibility(View.GONE);
                                finish();
                            }
                            
                            @Override
                            public void onFailure(int arg0, String arg1)
                            {
                                LogUtil.e("失败===");
                                pBLoad.setVisibility(View.GONE);
                                
                            }
                        });
                        
                    }
                    
                    @Override
                    public void onFailure(int arg0, String arg1)
                    {
                        pBLoad.setVisibility(View.GONE);
                        
                    }
                });
                
            }
        });
        tvDeil = (EditText)findViewById(R.id.ivDetil);
    }
    
    @Override
    public void loadFilePath(String path)
    {
        this.path = path;
        ImageLoader.getInstance().displayImage("file://" + path, ivPhoto, DogApplication.options);
    }
    
    
}
